package com.front.managementfront.agreement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.alibaba.fastjson.JSON;
import com.front.managementfront.agreement.entity.HktsAgreement;
import com.front.managementfront.agreement.service.IHktsAgreementService;
import com.front.managementfront.utils.CustomPageUtils;
import com.hkts.management.common.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;

import java.time.LocalDateTime;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author yck
 * @since 2021-08-01
 */
@RestController
@RequestMapping("/front/hktsAgreement")
@Api(tags = "合同协议接口")
public class HktsAgreementController {

    @Autowired
    public IHktsAgreementService iHktsAgreementService;

    /**
     * 分页查询数据
     *
     * @param page          分页信息
     * @param hktsAgreement 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHktsAgreementPageList")
    @ApiOperation(value = "合同分页")
    public Result<CustomPage<HktsAgreement>> getHontUserList(Page<HktsAgreement> page, HktsAgreement hktsAgreement) {
        return ResultUtil.data(iHktsAgreementService.selectPage(page, hktsAgreement));
    }

    /**
     * 保存和修改公用的
     *
     * @param hktsAgreement 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsAgreementSave")
    @ApiOperation(value = "新增及保存")
    public Result<Object> hktsAgreementSave(@RequestBody HktsAgreement hktsAgreement) {
        if(StringUtils.isBlank(hktsAgreement.getId()) ){
            hktsAgreement.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
        }
        if(StringUtils.isBlank(hktsAgreement.getIsShow())){
            hktsAgreement.setIsShow("0");
        }
        if(hktsAgreement.getOrderField()==null){
            //获取最大的排序号
            hktsAgreement.setOrderField(iHktsAgreementService.getMaxOrder());
        }
        if(hktsAgreement.getCreateTime()==null){
            hktsAgreement.setCreateTime(LocalDateTime.now());
        }
        hktsAgreement.setUpdateTime(LocalDateTime.now());
        iHktsAgreementService.saveOrUpdate(hktsAgreement);
        return ResultUtil.success("保存成功");
    }


    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hktsAgreementGetById")
    public Result<String> hktsAgreementGetById(String id,String contractId) {
       String content= iHktsAgreementService.hktsAgreementGetById(id,contractId);
        return ResultUtil.data(content);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsAgreementDelete")
    public Result<Object> hontUserDelete(String id) {
        iHktsAgreementService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsAgreementBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHktsAgreementService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }
    /** 功能描述:
    *    绑定运输协议绑定货物类别
    * @return:
    * @Author: 杨春科
    * @Date: 2021-08-31 11:30
    * @Description //TODO
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/boundGoodsCategory")
    public Result<String> boundGoodsCategory(String agreementId,String[] itemIds) {
        return   iHktsAgreementService.boundGoodsCategory(agreementId,itemIds);
    }
    /** 功能描述:
     *    协议显示状态修改
     * @return:
     * @Author: 杨春科
     * @Date: 2021-08-31 11:30
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateShowStatus")
    public Result<String> updateShowStatus(String id,String isShow) {
        HktsAgreement byId = iHktsAgreementService.getById(id);
        byId.setIsShow(isShow);
        byId.setUpdateTime(LocalDateTime.now());
        iHktsAgreementService.updateById(byId);
        return ResultUtil.success("修改成功");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateOrderFiled")
    public Result<String> updateOrderFiled(String id,Integer orderField) {
        HktsAgreement byId = iHktsAgreementService.getById(id);
        byId.setOrderField(orderField);
        byId.setUpdateTime(LocalDateTime.now());
        iHktsAgreementService.updateById(byId);
        return ResultUtil.success("修改成功");
    }
    /** 功能描述:
    *  对外开发的协议列表
    * @return:
    * @Author: 杨春科
    * @Date: 2021-09-01 15:15
    * @Description //TODO
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/agrementList")
    public Result<List<HktsAgreement>> agrementList() {
        List<HktsAgreement>   list=iHktsAgreementService.agrementList();
        return ResultUtil.data(list);
    }

}
